<?php

if (!isset($_SESSION)) {
    session_start();
}

class contact extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    protected $configs;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('smarty3', '', 'smarty');
        $this->load->model('pushclimb/contact_model');
        $this->load->model('admin-planners/setting_model');
        $this->contact_model = new contact_model();
        $this->setting_model=new setting_model();
    }

    public function index() {
        
        $map2 = $this->setting_model->getByKey("find-us-on-the-map");
        $Data["map2"]=$map2[0];
        $contactinfo = $this->setting_model->getByKey("contact-information");
        $Data["contactinfo"]=$contactinfo[0];
        $this->smarty
                ->assign('_SESSION', $_SESSION)
                ->assign('Data', $Data)
                ->display("pushclimbing/pages/13_contact");
    }

    function addContact() {
        $msgs = array();
        if ((!isset($_POST["Name"])) || $_POST["Name"] == "") {
            $msgs[] = "Name does not empty.";
        }
        if ((!isset($_POST["Email"])) || $_POST["Email"] == "") {
            $msgs[] = "Email does not empty.";
        }
        if (isset($_POST["Email"]) && preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["Email"]) === 0) {
            $msgs[] = "Email is not valid.";
        }
        if ((!isset($_POST["Subject"])) || $_POST["Subject"] == "") {
            $msgs[] = "Subject does not empty.";
        }
        if ((!isset($_POST["Message"])) || $_POST["Message"] == "") {
            $msgs[] = "Message does not empty.";
        }
        
        if (count($msgs) > 0) {
            $code = -44;
            $msg = "";
            foreach ($msgs as $m) {
                $msg.="$m<br/>";
            }
        } else {

            $Params = array(
                "FullName" => strip_tags($_POST["Name"]),
                "Subject" => strip_tags($_POST["Subject"]),
                "Message" => strip_tags($_POST["Message"]),
                "Email" => strip_tags($_POST["Email"])
            );
            //print_r($Params);return;
            if ($this->contact_model->insert($Params)) {
                $code = 1;
                $msg = "Success. Thank you for contact.";
            } else {
                $code = -1;
                $msg = "Fail. Please try again.";
            }
        }
        echo json_encode(array("code" => $code, "msg" => $msg));
    }

}

